import datetime
import time
from apscheduler.schedulers.background import BackgroundScheduler
# from app import machines as cpu
import requests
import response
from flask_script import Manager
from app.machines.mac import MacInfo
from app.machines.machine import MachineInfo
from app.machines.cpu import CpuInfo
from app.machines.memory import MemoryInfo
from app.machines.disk import DiskInfo
from app.machines.network import NetworkInof
from flask import Flask
from multiprocessing import Process

import json

# schedulex = Flask(__name__)

api_address = "http://8.131.87.108:6000/hcapi"

def task():
    cpu_subtask(api_address)
    memory_subtask(api_address)
    disk_subtask(api_address)
    network_subtask(api_address)

def task_once():
    print("begin get machine info.....")
    machine_subtask()
    print("end get machine info.###")


def machine_subtask():
    data = {}
    data["maccode"] = MacInfo.get_mac()
    data["hardwarecode"] = "machine"
    data["data"] = json.dumps(MachineInfo.get_machine_info())

    response = requests.post(api_address,data=data)
    print(response.status_code)


def cpu_subtask(api_address):
    data = {}
    data['maccode'] = MacInfo.get_mac()
    data['hardwarecode'] = 'cpu'
    data['data'] = json.dumps(CpuInfo.get_cpu_info())
    r = requests.post(api_address,data=data)
    if r.status_code == 200 and r.text != '':
        print(r.text)
    else:
        print(r.status_code)

def memory_subtask(api_address):
    data = {}
    data['maccode'] = MacInfo.get_mac()
    data['hardwarecode'] = 'memory'
    data['data'] = json.dumps(MemoryInfo.get_memory_info())
    r = requests.post(api_address,data=data)
    if r.status_code == 200 and r.text != '':
        print(r.text)
    else:
        print(r.status_code)

def disk_subtask(api_address):
    data = {}
    data['maccode'] = MacInfo.get_mac()
    data['hardwarecode'] = 'disk'
    data['data'] = json.dumps(DiskInfo.get_disk_info())
    r = requests.post(api_address,data=data)
    if r.status_code == 200 and r.text != '':
        print(r.text)
    else:
        print(r.status_code)

def network_subtask(api_address):
    data = {}
    data['maccode'] = MacInfo.get_mac()
    data['hardwarecode'] = 'network'
    data['data'] = json.dumps(NetworkInof.get_network_info())
    r = requests.post(api_address,data=data)
    if r.status_code == 200 and r.text != '':
        print(r.text)
    else:
        print(r.status_code)


if __name__ == '__main__':
    # 创建后台执行的 schedulers
    scheduler = BackgroundScheduler()
    # 添加调度任务
    # 调度方法为 timedTask，触发器选择 interval(间隔性)，间隔时长为 1 秒
    scheduler.add_job(task, 'interval', seconds=2)
    # print(datetime.datetime.now())

    # 只启动后第5秒执行一次
    deadline = datetime.datetime.now().replace(microsecond=0) + datetime.timedelta(seconds=5)
    scheduler.add_job(task_once, 'date',run_date=deadline)

    # scheduler.add_job(task_once,'date',time_tuple,'xx',100,'初始化')
    # 启动调度任务
    scheduler.start()
    while True:
        # print(time.time())
        time.sleep(1)
    # schedulex.run(debug=True,host='0.0.0.0', port=5000)